/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.httpservice;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.vo.RequestVo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class HttpSender {

    static Logger logger = Logger.getLogger(HttpSender.class.getName());
    public String request;
    public String url;
    public LinkedBlockingQueue<RequestVo> blockingQueue;

    public HttpSender() {
    }

    public HttpSender(String request, String url) {
        this.request = request;
        this.url = url;
    }

    public HttpSender(String request, String url, LinkedBlockingQueue<RequestVo> blockingQueue) {
        this.request = request;
        this.url = url;
        this.blockingQueue = blockingQueue;
    }

    public LinkedBlockingQueue<RequestVo> getBlockingQueue() {
        return blockingQueue;
    }

    public void setBlockingQueue(LinkedBlockingQueue<RequestVo> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    //测试查重
    public String sendStringByUrl(String request, String url) {
        String result = "";
        HttpPost httppost1 = null;
        HttpClient httpClient1 = null;
        try {
            httpClient1 = new DefaultHttpClient();
            //设置超时5s
            httpClient1.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
            httpClient1.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000);
            httppost1 = new HttpPost(url);
            StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
            reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
            httppost1.setEntity(reqEntity);
            HttpResponse response = httpClient1.execute(httppost1);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                String rs = HttpResponseParser.getResponseContent(response);
                result = rs;
            } else {
                result = "";
                logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            httppost1.releaseConnection();
            httpClient1.getConnectionManager().shutdown();
        }
        return result;
    }

    public InputStream picInputStream(String requestStr, String postUrl) {
        InputStream inputStream = null;
        HttpPost httppost = null;
        logger.info(requestStr + "======>>" + postUrl);
        HttpClient httpClient = new DefaultHttpClient();
        try {
            //默认字符编码是ISO-8859-1
            httppost = new HttpPost(postUrl);
            InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(requestStr.getBytes("UTF-8")), -1);
            reqEntity.setContentType("application/octet-stream");// 区分不同种类的数据,xml数据
            reqEntity.setContentEncoding("UTF-8");// charset=UTF-8
            httppost.setEntity(reqEntity);
            logger.info("开始发送");
            HttpResponse response = httpClient.execute(httppost);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                inputStream = response.getEntity().getContent();
                logger.info("200" + inputStream.available());
            } else {
                inputStream = response.getEntity().getContent();
                logger.info("下载完成" + inputStream.available());
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
//            httppost.releaseConnection();
//            httpClient.getConnectionManager().shutdown();
        }
        return inputStream;
    }

    public static String senRequest(String request, String url) {
        String result = "";
        HttpPost httppost1 = null;
        HttpClient httpClient1 = null;
        try {
            httpClient1 = new DefaultHttpClient();
            //设置超时5s
            httpClient1.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 500);
            httpClient1.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 500);
            httppost1 = new HttpPost(url);
            StringEntity reqEntity = new StringEntity(request, Charset.forName("UTF-8"));
            reqEntity.setContentType("application/json");// 区分不同种类的数据,xml数据
            httppost1.setEntity(reqEntity);
            HttpResponse response = httpClient1.execute(httppost1);
            StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() == 200) {
                String rs = HttpResponseParser.getResponseContent(response);
                result = rs;
            } else {
                result = "";
                logger.warn("response code is " + sl.getStatusCode() + "\nresponse content is \t" + result); //                System.out.println(response.toString());
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            httppost1.releaseConnection();
            httpClient1.getConnectionManager().shutdown();
        }
        return result;

    }

}
